<?php

namespace app\common\model;

class News extends Base
{
    protected $name = 'web_news';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    // 定义时间戳字段名
    protected $dateFormat  = 'Y-m-d';
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    // 追加属性
    protected $append = [
    ];

    /**
     * is_release
     * RELEASE_WAIT 未发布
     * RELEASE_YAT 已发布
     */
    const RELEASE_WAIT = 0;
    const RELEASE_YAT = 1;

    /**
     * type
     * TYPE_TEXT 企业动态
     * TYPE_LINK 行业动态
     */
    const TYPE_TEXT = 1;
    const TYPE_HANG = 2;

    public function limitList($type,$page = 1,$limit = 3){
        return $this->where('type',$type)->where('is_release',self::RELEASE_YAT)->order('id desc')->limit($page - 1, $page * $limit)->select();
    }

    public function category()
    {
        return $this->belongsTo('NewsCategory', 'type', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * 上一篇
     */
    public function prevData($id, $type = ""){
        if($type){
            $this->where('type',$type);
        }
        return $this->where('is_release',self::RELEASE_YAT)->where('id','<',$id)->field('id,type,title,en_title')->order('weigh desc,id desc')->find();
    }
    /**
     * 下一篇
     */
    public function nextData($id, $type = ""){
        if($type){
            $this->where('type',$type);
        }
        return $this->where('is_release',self::RELEASE_YAT)->where('id','>',$id)->field('id,type,title,en_title')->order('weigh asc,id asc')->find();
    }
}